# docker 相关：
alias dr='docker run'
alias drd='docker run -d'

alias dritd='f() { docker run -itd --restart=always -p "$3":"$3" --name "$1" "$2" /bin/sh };f' # --restart=always的作用是跟随docker启动。

alias drit='docker run -it'
alias db='docker build'

alias dps='docker ps -s'
alias dpsa='docker ps -as'

# 镜像相关:
alias dimgs='docker images'
dimgdel() { # 删除所有image:
    if [ $# != 1 ]; then
        docker rmi $(docker images -q) # 删除所有image
        return
    fi
    image=$1
    docker rmi $image
}
# 容器相关:
alias dcons='sudo docker ps -s'
alias dconsa='sudo docker ps -as'
# 删除指定端口号docker容器:
dcondel() {
    if [ $# != 1 ]; then
        docker stop $(docker ps -aq)
        docker rm -f $(docker ps -aq)
        return
    fi

    port=$1
    # 如果输入的是容器名称或者id:
    re='^[0-9]+$'
    if ! [[ $port =~ $re ]]; then
        docker stop "${port}"
        docker rm "${port}"
        return
    fi
    # 如果输入的是端口号:
    for id in $(docker ps -q); do
        if [[ $(docker port "${id}") == *"${port}"* ]]; then
            echo "stopping container ${id}"
            docker stop "${id}"
            docker rm "${id}"
        fi
    done
}

alias dconstp='docker stop'

# docker ps --format "{{.Names}}"

dconstp.port() {
    if [ $# != 1 ]; then
        echo "#### 使用方法: dconstp.port 80"
        return
    fi
    port=$1
    for name in $(docker ps --format "{{.Names}}"); do
        if [[ $(docker port "${name}") == *"${port}"* ]]; then
            echo "stopping container ${name}"
            docker stop "${name}"
        fi
    done
}

dconstt.port() {
    if [ $# != 1 ]; then
        echo "#### 使用方法: dconstt.port 80"
        return
    fi
    port=$1
    for name in $(docker ps -a --format "{{.Names}}"); do
        if [[ $(docker port "${name}") == *"${port}"* ]]; then
            echo "starting container ${name}"
            docker start "${name}"
        fi
    done
}

alias dconstt='docker start'
alias dconrstt='docker restart'

# 进入容器:
dshell() {
    container_name=$1
    docker exec -it $container_name sh
}

# 网络相关:
alias dnet='docker network'
alias dnets='docker network ls'

alias dupdate='docker update'

alias ddf='docker system df'
alias dll='docker pull'
alias dsh='docker push'

# docker ps --format "table {{.Image}}  {{.Command}} {{.CreatedAt}} {{.Status}} {{.Ports}} {{.Names}}"

dins() {
    docker inspect $1
}
# 查看容器ip:
dinsip() {
    container_name=$1
    docker inspect -f '{{range.NetworkSettings.Networks}}ip:{{.IPAddress}}  gateway:{{.Gateway}}{{end}}' $container_name
}

# 查看容器cpu内存:
dsta() {
    docker stats --format "table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}"
}

# 查看容器ip:
dconip() {
    container_name=$1
    docker inspect -f '{{range.NetworkSettings.Networks}}ip:{{.IPAddress}}  gateway:{{.Gateway}}{{end}}' $container_name
}

# 清理磁盘空间:
dclean() {
    docker system prune --all --volumes --force
}

# 删除所有 build cache:
dbuildclean() {
    docker builder prune --all -f
}

# 查看容器日志，动态
dlogs() {
    if [ $# != 1 ]; then
        sudo docker logs -f --tail=50
        # docker logs -f -t --tail=50
        return
    fi
    container_name=$1
    sudo docker logs -f -t --tail=50 $container_name
}

alias din='docker login -u goybltpc'
alias da='docker attach'

alias dcup="docker-compose up -d --build"

# docker-compose 相关：

dce() {
    if [ $# != 1 ]; then
        echo "example: dce nginx"
        return
    fi
    docker-compose exec $1 bash
}

dcu() {
    if [ $# != 0 ]; then
        docker-compose -f $1 up --build
        return
    fi
    docker-compose up --build
}

dcr() {
    if [ $# != 1 ]; then
        docker-compose -f $1 run --rm $2
    fi
    docker-compose run --rm $1
}

dcd() {
    if [ $# != 0 ]; then
        docker-compose -f $1 down
        return
    fi
    docker-compose down
}

# dclogs() {
#     docker-compose logs -f $1
# }

alias dc='docker-compose'
alias dclogs='docker-compose logs -f'

dexb() {
    docker exec -it $1 bash
}

dex() {
    docker exec -it $1 $2
}

dlog() {
    docker logs $1
}

# docker stacks相关:
dsks() {
    docker stack ls
}

dnodes() {
    docker node ls
}

dsers() {
    docker service ls
}
